package com.kgcx.system.rabbitmq.producer;

import com.alibaba.fastjson.JSONObject;
import com.kgcx.common.rabbitmq.bean.Message;
import com.kgcx.common.rabbitmq.consts.ExchangeConst;
import com.kgcx.common.rabbitmq.consts.RoutingKeyConst;
import com.kgcx.core.utils.JsonUtil;
import com.kgcx.system.domain.BaseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 吴刚林
 * @date 2017/11/20
 */
@Slf4j
@Component
public class BaseInfoProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    private static final String TYPE = "baseInfoCompany";

    public void addBaseInfo(BaseInfo baseInfo) {
        log.info(String.format("新增运营公司记录新增成功的MQ消息，公司id[%s]。", baseInfo.getCompanyId()));
        Message msg = Message.buildMessage(TYPE, Message.FLAG_ADD, baseInfo);
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON, JSONObject.toJSONString(msg));
    }

    public void updateBaseInfo(BaseInfo baseInfo) {
        log.info(String.format("修改运营公司记录新增成功的MQ消息，公司id[%s]。", baseInfo.getCompanyId()));
        Message msg = Message.buildMessage(TYPE, Message.FLAG_UPDATE, baseInfo);
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON,
                JsonUtil.toString(msg));
    }

    public void deleteBaseInfo(BaseInfo baseInfo) {
        log.info(String.format("删除运营公司记录新增成功的MQ消息，公司id[%s]。", baseInfo.getCompanyId()));
        Message msg = Message.buildMessage(TYPE, Message.FLAG_DELETE, baseInfo);
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON,
                JsonUtil.toString(msg));
    }


}
